
#include "stdafx.h"
#include "script_editor.h"
#include "script_editor_new_script.h"

static const QColor indexUsedColor = QColor(167, 211, 92);

ScriptEditor::ScriptEditor(QWidget* pwgt) 
	: QDialog(pwgt, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
{
	ui.setupUi(this);

	connect(ui.bAccept, SIGNAL(clicked), this, SLOT(on_bAccept_clicked));
	connect(ui.bReject, SIGNAL(clicked), this, SLOT(on_bReject_clicked));
	connect(ui.bAdd, SIGNAL(clicked), this, SLOT(on_bAdd_clicked));
	connect(ui.bEdit, SIGNAL(clicked), this, SLOT(on_bEdit_clicked));
	connect(ui.bRemove, SIGNAL(clicked), this, SLOT(on_bRemove_clicked));
	connect(ui.bUp, SIGNAL(clicked), this, SLOT(on_bUp_clicked));
	connect(ui.bDown, SIGNAL(clicked), this, SLOT(on_bDown_clicked));
	connect(ui.bUse, SIGNAL(clicked), this, SLOT(on_bUse_clicked));
	connect(ui.script_list, SIGNAL(currentRowChanged(int)), this, SLOT(displayDescription(int)));
	ui.script_list->setStyleSheet("alternate-background-color: rgb(230, 230, 230)");
}

void ScriptEditor::setData(const vector<pair<QString, QString>> &script_list, const  QString &current_script) {
	for (int i = 0; i < script_list.size(); i++){
		QString name = script_list[i].first;
		ui.script_list->addItem(name);
		description.push_back(script_list[i].second);
		if (name == current_script) ui.script_list->item(i)->setBackgroundColor(indexUsedColor);
	}
}

vector<pair<QString, QString>> ScriptEditor::getScriptList(){
	if (description.size() != ui.script_list->count())
		QMessageBox::information(0, tr("Get script list."), tr("Different sizes of data!"), QMessageBox::Ok);	
	vector<pair<QString, QString>> list;
	for (int i = 0; i < ui.script_list->count(); i++)
		list.push_back(make_pair(ui.script_list->item(i)->text(), description[i]));
	return list;
}

QString ScriptEditor::getCurrentScript(){
	for (int i = 0; i < ui.script_list->count(); i++)
		if (ui.script_list->item(i)->backgroundColor() == indexUsedColor)
			return ui.script_list->item(i)->text();
	return "";
}

void ScriptEditor::on_bAccept_clicked(){
	accept();
}

void ScriptEditor::on_bReject_clicked(){
	int result = QMessageBox::information(this, "Close", 
		"Close the form? any unsaved data will be lost", 
		QMessageBox::Yes | QMessageBox::Default, 
		QMessageBox::No, 
		QMessageBox::Cancel | QMessageBox::Escape); 
	if (result == QMessageBox::Yes) 
		reject();
}

void ScriptEditor::on_bAdd_clicked(){
	ScriptEditorNewScript script_editor_new_script(this);
	if (script_editor_new_script.exec()){
		QString name = script_editor_new_script.getName();
		if (isUniqueName(name) == false) return;
		ui.script_list->addItem(name);
		description.push_back(script_editor_new_script.getDescription());
		displayDescription(description.size() - 1);
	}
}

void ScriptEditor::on_bEdit_clicked(){
	int index = ui.script_list->currentRow();
	if (index == -1) return;
	ScriptEditorNewScript script_editor_new_script(this);
	script_editor_new_script.setData(ui.script_list->currentItem()->text(), description[index]);
	if (script_editor_new_script.exec()){
		QString name = script_editor_new_script.getName();
		if (isUniqueName(name, index) == false) return;
		ui.script_list->currentItem()->setText(name);
		description[index] = script_editor_new_script.getDescription();
		displayDescription(index);
	}
}

void ScriptEditor::on_bRemove_clicked(){
	if (ui.script_list->count() <= 1){
		QMessageBox::information(0, tr("Removing script..."), tr("You can't remove the last script!"), QMessageBox::Ok);	
		return;
	}
	int index = ui.script_list->currentRow();
	if (index == -1){
		QMessageBox::information(0, tr("Removing script..."), tr("Script is not selected!"), QMessageBox::Ok);	
		return;
	}

	if(QMessageBox::question(0, tr("Removing script..."), QString(tr("Remove script: '%1'?")).arg(ui.script_list->currentItem()->text()),
		QMessageBox::Yes |QMessageBox::No) == QMessageBox::Yes)
	{	
		description.erase(description.begin() + index);
		delete ui.script_list->takeItem(index);
		displayDescription(ui.script_list->currentRow());
	}
}

void ScriptEditor::on_bUse_clicked(){
	for(int i = 0; i < ui.script_list->count(); i++){
		if (i == ui.script_list->currentRow()){
			ui.script_list->item(i)->setBackgroundColor(indexUsedColor);
		}else{
			ui.script_list->item(i)->setBackgroundColor(Qt::white);
		}
	}
}

bool ScriptEditor::isUniqueName(const QString &name, const int &index){
	for (int i = 0; i < ui.script_list->count(); i++)
		if(ui.script_list->item(i)->text() == name && index != i){
			QMessageBox::critical(0, tr("Error"), tr("That name already exists!"), QMessageBox::Ok);	
			return false;
		}
	return true;
}

void ScriptEditor::displayDescription(int index){
	int _index = ui.script_list->currentRow();
	QString text, prefix = "Description:\n";
	if (_index != -1 && _index < description.size()) text = description[_index];
	ui.lblDescr->setText(prefix + text);
}

void ScriptEditor::on_bUp_clicked(){
	int index = ui.script_list->currentRow();
	if (index == -1 || index == 0) return;
	QListWidgetItem *item = ui.script_list->currentItem();
	QString current_description = description[index];
	ui.script_list->removeItemWidget(ui.script_list->takeItem(index));
	ui.script_list->insertItem(index - 1, item);
	description.erase(description.begin() + index);
	description.insert(description.begin() + index - 1, current_description);
	ui.script_list->setCurrentRow(index - 1);
}

void ScriptEditor::on_bDown_clicked(){
	int index = ui.script_list->currentRow();
	if (index == -1 || index == ui.script_list->count() - 1) return;
	QListWidgetItem *item = ui.script_list->currentItem();
	QString current_description = description[index];
	ui.script_list->removeItemWidget(ui.script_list->takeItem(index));
	ui.script_list->insertItem(index + 1, item);
	description.erase(description.begin() + index);
	description.insert(description.begin() + index + 1, current_description);
	ui.script_list->setCurrentRow(index + 1);
}